import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
import path from 'path'
import tsconfigPaths from 'vite-tsconfig-paths';
import tailwind from 'tailwindcss'
import autoprefixer from 'autoprefixer'
import commonjs from '@rollup/plugin-commonjs'
export default defineConfig({
  css: {
    postcss: {
        plugins: [tailwind(), autoprefixer()]
    }
  },
  plugins: [
    vue(),
    tsconfigPaths(),
    commonjs({
      requireReturnsDefault: "auto"
    })
  ],
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src')
    },
    extensions: ['.css', '.scss', '.vue', '.json', '.ts', '.tsx']
  },
  build: {
    lib: {
      entry: path.resolve(__dirname, 'installUi.ts'),
      name: 'cocoaVueUi',
      fileName: (format) => `cocoa-vue-ui.${format}.js`,
      formats: ['es']
    },
    rollupOptions: {
      external: ['vue'],
      output: {
        globals: { vue: 'Vue' },
        // preserveModules: true,
        preserveModulesRoot: 'src',
        exports: 'named',
        chunkFileNames: 'components/[name]-[hash].js', // 统一组件chunk路径
        manualChunks(id) {
          if (id.includes('src/components')) {
            return id.split('src/components/')[1].split('/')[0]
          }
        }
      }
    }
  //   lib: {
  //     entry: 'installUi.ts',
  //     name: 'cocoaVueComponentUi',
  //     fileName: (format) => `cocoa-vue-ui.${ format }.ts`,
  //     formats: ['es', 'umd']
  // },
  // rollupOptions: {
  //     external: ['vue',
  //          ],
  //     output: {
  //         globals: {
  //             vue: 'Vue',
  //         }
  //     }
  // }
  },
});